package com.example.zyl.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zyl.api.entity.SysDict;
import com.example.zyl.api.entity.dto.SysDictDto;
import com.example.zyl.api.service.SysDictService;
import com.example.zyl.common.CommonResult;
import com.example.zyl.common.query.annotation.ApiLog;
import com.example.zyl.common.query.enums.BusinessType;
import com.example.zyl.common.query.page.PageParam;
import com.example.zyl.common.query.page.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (SysDict)表控制层
 *
 * @author zyl
 * @since 2025-01-14 13:58:48
 */

@RestController
@RequestMapping("sysDict")
@RequiredArgsConstructor
public class SysDictController {
    /**
     * 服务对象
     */
    private final SysDictService sysDictService;
    
     /**
     * 分页查询
     *
     * @param pageParam 筛选条件
     * @return 查询结果
     */
    @GetMapping(value = "/queryByPage")
    @ApiLog(module = "查询列表", businessType = BusinessType.SELECT)
    public PageResult<List<SysDict>> queryByPage(@RequestBody PageParam<SysDict, SysDictDto> pageParam) {
        Page<SysDict> page = this.sysDictService.queryByPage(pageParam);
        return PageResult.pageResult(page);
    }
    

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiLog(module = "查询单条", businessType = BusinessType.SELECT)
    public CommonResult<SysDict> queryById(@PathVariable("id") Long id) {
        return CommonResult.success(this.sysDictService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysDict 实体
     * @return 新增结果
     */
    @PostMapping
    public CommonResult<SysDict> add(SysDict sysDict) {
        return CommonResult.supplier(() -> this.sysDictService.insert(sysDict));
    }

    /**
     * 编辑数据
     *
     * @param sysDict 实体
     * @return 编辑结果
     */
    @PutMapping
    public CommonResult<SysDict> edit(SysDict sysDict) {
        return CommonResult.supplier(() -> this.sysDictService.update(sysDict));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public CommonResult<Boolean> deleteById(Long id) {
        return CommonResult.supplier(() -> this.sysDictService.deleteById(id));
    }
    
}

